<?php
//$this->headTitle('Edit Employment US E2Admin (E2A) Version 1.0 - E2com company');
$e_author = $_SESSION['a_username'];
$entryid = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$e_type = '4';

if ($entryid > 0) {

    $rows=Db::getOneRow("*", 'entry', 'e_type = 4 AND entryid='.$entryid);

    if (($_SESSION['a_access'] == 2 && $e_author != $rows['e_author']) || $_SESSION['a_access'] < 2 || $_SESSION['a_group'] != 1) {
        Js::alert(fl('err_access'));
        Js::gourl(baseUrl() . '/product');
        exit;
    }
    if (isset($_GET['del']) && '1' === $_GET['del']) {
        if(Db::delete('entry',"entryid=$entryid")){
            Js::gourl(baseUrl() . '/product');
            exit;
        }
    }

} elseif ($_SESSION['a_access'] < 2 || $_SESSION['a_group'] != 1) {

    Js::gourl(baseUrl() . '/product');
    exit;
}

if (Http::isPost()) {

    $ignoredFields = array();

    $e_date_posted=date('Y').'-'.date('m').'-'.date('d');

    if (Lang::has('vn')) {
        $e_title = substr(trim(Form::input('e_title','ne')), 0, 200);
        $e_content = Form::input('e_content','ne') . '<span style="display:none"> Chỉnh sửa bởi: '.$e_author.' vào lúc '.date("H:i - m/d/y",Date::toLocalTime(Date::getGmtTime(), Config::get('TimeZone'))).'</span>';
    } else {
        $ignoredFields[] = 'e_title';
        $ignoredFields[] = 'e_content';
    }

    if (Lang::has('en')) {
        $e_title2 = substr(trim(Form::input('e_title2','ne')), 0, 200);
        $e_content2 = Form::input('e_content2','ne') . '<span style="display:none"> Edit by: '.$e_author.' at '.date("H:i - m/d/y",Date::toLocalTime(Date::getGmtTime(), Config::get('TimeZone'))).'</span>';
    } else {
        $ignoredFields[] = 'e_title2';
        $ignoredFields[] = 'e_content2';
    }

    $e_status = Form::input('e_status');

    if(Form::isValid()) {// Check 0 loi

        if($entryid > 0 ) { //-------Edit

            eval(Db::update('entry', "entryid=$entryid", array_merge(
                array('e_author','e_type','entryid', 'e_date_expire'),
                $ignoredFields)));

            if(mysql_affected_rows() > 0) {
                Js::alert(fl('success_update'));
                Js::GoUrl(baseUrl() . '/product'); exit();
            }

        } else {

            eval(Db::insert('entry', array_merge(
                array('entryid', 'e_date_expire'),
                $ignoredFields)));
            
            if(mysql_insert_id() > 0) {
                Js::alert(fl('success_insert'));
                Js::GoUrl(baseUrl() . '/product'); exit();
            }
        }
    }    
    $rows = $_POST;
} elseif ($entryid < 1) {    
    $rows = array(        
        'e_content' => '',
        'e_content2' => '',
        'e_title' => '',
        'e_title2' => '',
        'e_status' => '1'
    );
}
